using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH2009
{
	[TestFixture]
	public class Fixture : BugTestCase
	{
		/// <summary>
		/// If unfixed (there is a patch for this attached to NH2009) this 
		/// test will generate SqlException because the were clause alias for property UserName will 
		/// refer to the table User and not the joined ADUser 
		/// This is because BlogPost has a many-to-one relation to User using property-ref that 
		/// refers to a property that belongs to a joined table 
		/// </summary>
		[Test]
		public void NoExceptionMustBeThrown()
		{
			var savedBlogPost = new BlogPost();

			using (ISession session = OpenSession())
			{
				var user1 = new User();
				user1.UserName = "User1";
				session.Save(user1);

				var user2 = new User();
				user2.UserName = "User2";
				session.Save(user2);

				savedBlogPost.Title = "Post 1";
				savedBlogPost.Poster = user1;
				session.Save(savedBlogPost);

				session.Flush();
			}
			using (ISession session = OpenSession())
			{
				var user = session.CreateQuery("from User u where u.UserName = 'User1'").List();
			}

			//using (ISession session = OpenSession())
			//{
			//  var user = session.Get<BlogPost>(savedBlogPost.ID);
			//}

			using (ISession session = OpenSession())
			{
				using (ITransaction tx = session.BeginTransaction())
				{
					session.Delete("from User");
					session.Delete("from BlogPost");
					tx.Commit();
				}
			}
		}
	}
}